using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormTextArea : CMFormElement
    {
        public override string Value
        {
            get
            {
                TextBox textbox = this.FindControl(string.Format("textbox_{0}", this.ID)) as TextBox;
                return textbox.Text;
            }
        }

        internal FormTextArea(Forms.Sdk.Items.TextArea element) : base(element)
        {
            System.Web.UI.WebControls.TextBox textbox = new TextBox();
            textbox.ID = string.Format("textbox_{0}", this.ID);
            textbox.TextMode = TextBoxMode.MultiLine;

            if (!string.IsNullOrEmpty(element.DefaultValue))
                textbox.Text = element.DefaultValue;

            if (element.MaxLength > 0)
                textbox.MaxLength = element.MaxLength;

            textbox.CssClass = string.Format("{0}Input", element.CSS);

            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            PlaceHolder phValidators = new PlaceHolder();
            Label redStar = new Label();
            redStar.ForeColor = Color.Red;
            redStar.Text = "* ";
            redStar.Visible = false;
            phValidators.Controls.Add(redStar);

            //Required
            if (element.Required)
            {
                redStar.Visible = true;    

                RequiredFieldValidator required = new RequiredFieldValidator();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = textbox.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label); 

                
                phValidators.Controls.Add(required);
            }

            //Regular expression
            if (!string.IsNullOrEmpty(element.RegularExpression))
            {
                redStar.Visible = true;    

                RegularExpressionValidator regular = new RegularExpressionValidator();
                regular.ID = string.Format("regular_{0}", this.ID);
                regular.ControlToValidate = textbox.ID;
                regular.ValidationGroup = element.FormID.ToString();
                regular.Display = ValidatorDisplay.Dynamic;
                regular.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("NotValid"), element.Label); 
                regular.ValidationExpression = element.RegularExpression;

                phValidators.Controls.Add(regular);
            }

            //Minimum length
            if (element.MinLength > 0)
            {
                redStar.Visible = true;    

                CustomValidator minimumLengthValidator = new CustomValidator();
                minimumLengthValidator.ID = string.Format("minimumLength_{0}", this.ID);
                minimumLengthValidator.ControlToValidate = textbox.ID;
                minimumLengthValidator.ValidationGroup = element.FormID.ToString();
                minimumLengthValidator.Display = ValidatorDisplay.Dynamic;
                minimumLengthValidator.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("MinLength"), element.Label, element.MinLength);
                minimumLengthValidator.ServerValidate += new ServerValidateEventHandler(minimumLengthValidator_ServerValidate);
                minimumLengthValidator.ValidateEmptyText = true;
                minimumLengthValidator.Attributes.Add("MinLength", element.MinLength.ToString());

                phValidators.Controls.Add(minimumLengthValidator);
            }

            //Maximum length
            if (element.MaxLength > 0)
            {
                redStar.Visible = true;

                CustomValidator maximumLengthValidator = new CustomValidator();
                maximumLengthValidator.ID = string.Format("maximumLength_{0}", this.ID);
                maximumLengthValidator.ControlToValidate = textbox.ID;
                maximumLengthValidator.ValidationGroup = element.FormID.ToString();
                maximumLengthValidator.Display = ValidatorDisplay.Dynamic;
                maximumLengthValidator.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("MaxLength"), element.Label, element.MaxLength); ;
                maximumLengthValidator.ServerValidate += new ServerValidateEventHandler(maximumLengthValidator_ServerValidate);
                maximumLengthValidator.ValidateEmptyText = false;
                maximumLengthValidator.Attributes.Add("MaxLength", element.MaxLength.ToString());

                phValidators.Controls.Add(maximumLengthValidator);
            }

            #endregion

            GenerateRow(element, label, textbox, phValidators);
        }

        protected void minimumLengthValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomValidator validator = source as CustomValidator;
            int minLength = 0;
            int.TryParse(validator.Attributes["MinLength"], out minLength);

            args.IsValid = args.IsValid && args.Value.Length >= minLength;
        }

        protected void maximumLengthValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomValidator validator = source as CustomValidator;
            int maxLength = 0;
            int.TryParse(validator.Attributes["MaxLength"], out maxLength);

            args.IsValid = args.IsValid && args.Value.Length <= maxLength;
        }
    }
}
